package ua.epam.muzalevskiy.traction.controllers;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import ua.epam.muzalevskiy.traction.commands.AddRouteCommand;
import ua.epam.muzalevskiy.traction.commands.AddStopCommand;
import ua.epam.muzalevskiy.traction.commands.AddStopToRouteCommand;
import ua.epam.muzalevskiy.traction.commands.AddVehicleToRouteCommand;
import ua.epam.muzalevskiy.traction.commands.ChangeLocaleToEnCommand;
import ua.epam.muzalevskiy.traction.commands.ChangeLocaleToRuCommand;
import ua.epam.muzalevskiy.traction.commands.Command;
import ua.epam.muzalevskiy.traction.commands.CreateVehicleCommand;
import ua.epam.muzalevskiy.traction.commands.DeleteRouteCommand;
import ua.epam.muzalevskiy.traction.commands.DeleteStopCommand;
import ua.epam.muzalevskiy.traction.commands.DeleteStopFromRouteCommand;
import ua.epam.muzalevskiy.traction.commands.DeleteVehicleCommand;
import ua.epam.muzalevskiy.traction.commands.DeleteVehicleFromRoute;
import ua.epam.muzalevskiy.traction.commands.GenerateRouteInfoCommand;
import ua.epam.muzalevskiy.traction.commands.GenerateRouteListCommand;
import ua.epam.muzalevskiy.traction.commands.GenerateScheduleCommand;
import ua.epam.muzalevskiy.traction.commands.LoginCommand;
import ua.epam.muzalevskiy.traction.commands.LogoutCommand;
import ua.epam.muzalevskiy.traction.commands.NoCommand;
import ua.epam.muzalevskiy.traction.commands.RegisterNewUserCommand;
import ua.epam.muzalevskiy.traction.commands.ViewAddVehicleToRoutePageCommand;
import ua.epam.muzalevskiy.traction.commands.ViewAdministrationPageCommand;
import ua.epam.muzalevskiy.traction.commands.ViewFindRoutePageCommand;
import ua.epam.muzalevskiy.traction.commands.ViewNewUserPageCommand;
import ua.epam.muzalevskiy.traction.commands.ViewNewVehiclePageCommand;
import ua.epam.muzalevskiy.traction.commands.ViewRouteInfoPageCommand;
import ua.epam.muzalevskiy.traction.commands.ViewSchedulePageCommand;

/**
 * <code>RequestHelper</code> class is used to get appropriate
 * <code>Command</code> by the <code>String</code> value.
 *
 * @author Muzalevskiy Vitaliy 
 * @see Command
 */
public class RequestHelper {
    private static RequestHelper instance = null;
    //logger is used to log RequestHelper class
    private static Logger logger = Logger.getLogger(RequestHelper.class);
    // defines a map with String as a key and Command as appropriate value
    private HashMap<String, Command> commands = new HashMap<String, Command>();

    private RequestHelper() {
        // defines avaliable commands for the web application
        commands.put("login", new LoginCommand());
        commands.put("add_route", new AddRouteCommand());
        commands.put("add_stop", new AddStopCommand());
        commands.put("add_stop_to_route", new AddStopToRouteCommand());
        commands.put("logout", new LogoutCommand());
        commands.put("add_vehicle_to_route", new AddVehicleToRouteCommand());
        commands.put("delete_route", new DeleteRouteCommand());
        commands.put("delete_stop", new DeleteStopCommand());
        commands.put("delete_stop_from_route", new DeleteStopFromRouteCommand());
        commands.put("delete_vehicle", new DeleteVehicleCommand());
        commands.put("delete_vehicle_from_route", new DeleteVehicleFromRoute());
        commands.put("generate_route_info", new GenerateRouteInfoCommand());
        commands.put("generate_route_list", new GenerateRouteListCommand());
        commands.put("generate_schedule", new GenerateScheduleCommand());
        commands.put("change_locale_en", new ChangeLocaleToEnCommand());
        commands.put("change_locale_ru", new ChangeLocaleToRuCommand());
        commands.put("register_new_user", new RegisterNewUserCommand());
        commands.put("view_new_user_page", new ViewNewUserPageCommand());
        commands.put("view_find_route_page", new ViewFindRoutePageCommand());
        commands.put("view_route_info_page", new ViewRouteInfoPageCommand());
        commands.put("view_schedule_page", new ViewSchedulePageCommand());
        commands.put("view_administratiom_page", new ViewAdministrationPageCommand());
        commands.put("view_new_vehicle_page", new ViewNewVehiclePageCommand());
        commands.put("create_vehicle", new CreateVehicleCommand());
        commands.put("view_add_vehicle_to_route_page", new ViewAddVehicleToRoutePageCommand());
        logger.debug("CommandExtractor created.");
    }

    /**
     * Returns Command by the specified String value
     * @param request value that corresponds to appropriate Command
     * @return Command by the specified String value
     */
    public Command getCommand(HttpServletRequest request) {
        String action = request.getParameter("command");
        logger.debug("action = " + action);
        Command command = commands.get(action);
        //if there is no key found sets default command
        if (command == null) {
            command = new NoCommand();
        }
        return command;
    }

    /**
     * Returns the only instance of the <code>RequestHelper</code>
     * (Singleton pattern)
     *
     * @return instance of the <code>RequestHelper</code>
     */
    public static RequestHelper getInstance() {
        if (instance == null) {
            instance = new RequestHelper();
        }
        return instance;
    } 
}
